package com.yanqu.road.dao.impl.giftKey;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.giftKey.UserGiftKeyRedPacket;

import java.sql.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class GiftKeyRedPacketDaoImpl extends TempDataObjectDao<UserGiftKeyRedPacket> {

    public static String getUserGiftKeyRedPacketSql = "select * from `t_u_gift_key_red_packet` where `userId`=?";
    public  Map<String, UserGiftKeyRedPacket> getUserGiftKeyRedPacket(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, UserGiftKeyRedPacket> userGiftKeyRedPacketMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserGiftKeyRedPacketSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserGiftKeyRedPacket userGiftKeyRedPacket = parse(rs);
                if (!userGiftKeyRedPacketMap.containsKey(userGiftKeyRedPacket.getKey())) {
                    userGiftKeyRedPacketMap.put(userGiftKeyRedPacket.getKey(), userGiftKeyRedPacket);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return userGiftKeyRedPacketMap;
    }

    public static String updateUserGiftKeyRedPacketSql = "update `t_u_gift_key_red_packet` set startTime=?,settings=?,lastReqTime=?,ingotsList=?,realGetIngots=?,getIngots=? where userId=? and exchangeCode=?";
    @Override
    protected boolean updateData(UserGiftKeyRedPacket userGiftKeyRedPacket) {
        userGiftKeyRedPacket.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userGiftKeyRedPacket.getStartTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, userGiftKeyRedPacket.getSettings()));
        para.put(i++, new DbParameter(Types.BIGINT, userGiftKeyRedPacket.getLastReqTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userGiftKeyRedPacket.getIngotsList())));
        para.put(i++, new DbParameter(Types.INTEGER, userGiftKeyRedPacket.getRealGetIngots()));
        para.put(i++, new DbParameter(Types.BOOLEAN, userGiftKeyRedPacket.isGetIngots()));
        para.put(i++, new DbParameter(Types.BIGINT, userGiftKeyRedPacket.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, userGiftKeyRedPacket.getKey()));
        if (getDb().executeNoneQuery(updateUserGiftKeyRedPacketSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserGiftKeyRedPacketSql = "insert into `t_u_gift_key_red_packet`(userId,exchangeCode,startTime,settings,lastReqTime,ingotsList,realGetIngots,getIngots) values (?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserGiftKeyRedPacket userGiftKeyRedPacket) {
        userGiftKeyRedPacket.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userGiftKeyRedPacket.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, userGiftKeyRedPacket.getKey()));
        para.put(i++, new DbParameter(Types.BIGINT, userGiftKeyRedPacket.getStartTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, userGiftKeyRedPacket.getSettings()));
        para.put(i++, new DbParameter(Types.BIGINT, userGiftKeyRedPacket.getLastReqTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userGiftKeyRedPacket.getIngotsList())));
        para.put(i++, new DbParameter(Types.INTEGER, userGiftKeyRedPacket.getRealGetIngots()));
        para.put(i++, new DbParameter(Types.BOOLEAN, userGiftKeyRedPacket.isGetIngots()));
        if (getDb().executeNoneQuery(insertUserGiftKeyRedPacketSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserGiftKeyRedPacket parseData(ResultSet rs) throws SQLException {
        UserGiftKeyRedPacket userGiftKeyRedPacket = new UserGiftKeyRedPacket();
        userGiftKeyRedPacket.setUserId(rs.getLong("userId"));
        userGiftKeyRedPacket.setKey(rs.getString("exchangeCode"));
        userGiftKeyRedPacket.setStartTime(rs.getLong("startTime"));
        userGiftKeyRedPacket.setSettings(rs.getString("settings"));
        userGiftKeyRedPacket.setLastReqTime(rs.getLong("lastReqTime"));
        userGiftKeyRedPacket.setIngotsList(JSON.parseObject(rs.getString("ingotsList"), new TypeReference<List<Integer>>() {}));
        userGiftKeyRedPacket.setRealGetIngots(rs.getInt("realGetIngots"));
        userGiftKeyRedPacket.setGetIngots(rs.getBoolean("getIngots"));

        return userGiftKeyRedPacket;
    }

}
